from core.support import set_cutscene
from core.item import Item
from core.action import Action
from core.support import get_game
import random

class Gun(Item):
    def __init__(self):
        Item.__init__(self, "gun")    # do basic initialization for every item
        self.bullets = 2

                
        # build list of actions
        self.add_action(Shoot)
    
    def get_description(self):
        if self.bullets ==2:
            return "A double-barreled shotgun (2)"
        elif self.bullets ==1:
            return "A double-barreled shotgun (1)"
        else:
            return "A double-barreled shotgun (empty)"

# define actions for this item

class Shoot(Action):
    def __init__(self):
        Action.__init__(self, "shoot")    # do basic initialization for every action
    
    # return description of action (used in label on webpage)
    def get_description(self):
        return "Shoot"
    
    # return http method to use when user clicks on this action
    # use "get" if just moving to another room.  if changing something
    # like the state of an inventory item or room then use "post"
    #def get_method(self):
        #return "post"
    
    def is_enabled(self):
        game = get_game()
        if game.history[-1].get_id() == 'lion' or game.history[-1].get_id() == 'lion_pride' or game.history[-1].get_id() == 'bush3':
            return False
        else:
            return True
        
    # only works in basement. add cutscene
    def do_action(self,game,room,request):
        if self.item.bullets >0:
            if room.get_id() == 'hunt':
                self.item.chance =random.randint(0,10)
                if self.item.chance >5 :
                    set_cutscene("You took that one down with ease. That will be good eating.")
                    self.item.bullets = self.item.bullets - 1
                elif self.item.chance ==3 or self.item.chance ==4:
                    set_cutscene("You got him, but a leopard stole your kill. You will probably die of starvation...or the leopard.")
                    self.item.bullets = self.item.bullets - 1
                else :
                    set_cutscene("You are a terrible shot and will probably die of starvation.")
                    self.item.bullets = self.item.bullets - 1           
            else:
                set_cutscene("You probably should have saved that bullet you idiot.")
                self.item.bullets = self.item.bullets - 1
        else:
            set_cutscene("You try to fire the gun, but realize it has no bullets.")
